<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"
            "http://www.w3.org/TR/REC-html40/loose.dtd">
<HTML>
<HEAD>



<META http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<META name="GENERATOR" content="hevea 1.08">
<LINK rel="stylesheet" type="text/css" href="umsroot.css">
<TITLE>
Non-logical Arrays
</TITLE>
</HEAD>
<BODY >
<A HREF="umsroot054.html"><IMG SRC ="previous_motif.gif" ALT="Previous"></A>
<A HREF="umsroot049.html"><IMG SRC ="contents_motif.gif" ALT="Up"></A>
<A HREF="umsroot056.html"><IMG SRC ="next_motif.gif" ALT="Next"></A>
<HR>

<H2 CLASS="section"><A NAME="htoc132">9.6</A>&nbsp;&nbsp;Non-logical Arrays</H2> 
<A NAME="@default480"></A>
Non-logical arrays are a generalisation of the non-logical variable, capable of storing
multiple values.
Arrays have to be declared in advance.
They have a fixed number of dimensions and a fixed size in each dimension.
Arrays in ECL<SUP><I>i</I></SUP>PS<SUP><I>e</I></SUP> are managed solely by special predicates.
In these predicates, arrays are represented by compound terms, e.g.
<B>matrix(5, 8)</B>
where <B>matrix</B> is the name of the array, the arity of 2 specifies
the number of dimensions, and the integers 5 and 8 specify the size
in each dimension. The number of elements this array can hold is 
thus 5*8 = 40.
The elements of this array can be addressed from <B>matrix(0,0)</B>
up to <B>matrix(4,7)</B>.<BR>
<BR>
An array must be explicitly created using a
<A HREF="../bips/kernel/modules/local-1.html"><B>local/1</B></A><A NAME="@default481"></A>
<A HREF="../bips/kernel/storage/array-1.html"><B>array/1</B></A><A NAME="@default482"></A>
declaration, e.g.
<BLOCKQUOTE CLASS="quote"><PRE CLASS="verbatim">
:- local array(matrix(5, 8)).
</PRE></BLOCKQUOTE>
The array is only accessible from within the module where it was declared.
The declaration will create a two-dimensional, 5-by-8 array with 40 elements
matrix(0,0) to matrix(4, 7).
Arrays can be erased using the predicate
<A HREF="../bips/kernel/storage/erase_array-1.html"><B>erase_array/1</B></A><A NAME="@default483"></A>, e.g.
<A NAME="@default484"></A>
<BLOCKQUOTE CLASS="quote"><PRE CLASS="verbatim">
erase_array(matrix/2).
</PRE></BLOCKQUOTE>
The value of an element of the array is set using the <A HREF="../bips/kernel/storage/setval-2.html"><B>setval/2</B></A><A NAME="@default485"></A>
<A NAME="@default486"></A>
predicate. The first argument of <A HREF="../bips/kernel/storage/setval-2.html"><B>setval/2</B></A><A NAME="@default487"></A> specifies the element which is
to be set, the second specifies the value to assign to it.
The goal
<BLOCKQUOTE CLASS="quote"><PRE CLASS="verbatim">
setval(matrix(3, 2), plato)
</PRE></BLOCKQUOTE>
sets the value
of element (3, 2) of array <TT>matrix</TT> to the atom <TT>plato</TT>.
Similarly, values of array elements are retrieved by use of the <A HREF="../bips/kernel/storage/getval-2.html"><B>getval/2</B></A><A NAME="@default488"></A>
<A NAME="@default489"></A>
predicate. The first argument of <A HREF="../bips/kernel/storage/getval-2.html"><B>getval/2</B></A><A NAME="@default490"></A> specifies the element to be
referenced, the second is unified with the value of that element.
Thus if the value of matrix(3, 2) had been set as above, the goal
<BLOCKQUOTE CLASS="quote"><PRE CLASS="verbatim">
getval(matrix(3, 2), Val)
</PRE></BLOCKQUOTE>
would unify <TT>Val</TT> with the atom <TT>plato</TT>.
Similarly to non-logical variables, the value of integer array elements
can be updated using <A HREF="../bips/kernel/storage/incval-1.html"><B>incval/1</B></A><A NAME="@default491"></A> and <A HREF="../bips/kernel/storage/decval-1.html"><B>decval/1</B></A><A NAME="@default492"></A>.<BR>
<BR>
It is possible to declare arrays whose elements are
constrained to belong to certain types. This allows ECL<SUP><I>i</I></SUP>PS<SUP><I>e</I></SUP> to increase
time and space efficiency of array element manipulation.
Such an array is created for instance by the predicate
<BLOCKQUOTE CLASS="quote"><PRE CLASS="verbatim">
:- local array(primes(100),integer).
</PRE></BLOCKQUOTE>
The second argument specifies the type of the elements of the array.
It takes as value an atom from the
list <TT>float</TT> (for floating point numbers),
<TT>integer</TT> (for integers), <TT>byte</TT> (an integer modulo 256),
or <TT>prolog</TT> (any Prolog term - the resulting array is the
same as if no type was specified).
When a typed array is created, the value of each element is initialised to zero
in the case of <TT>byte</TT>, <TT>integer</TT> and <TT>float</TT>, and to
an uninstantiated variable in the case of <TT>prolog</TT>.
Whenever a typed array element is set, type checking is carried out.<BR>
<BR>
As an example of the use of a typed array, consider the following goal, which
creates a 3-by-3 matrix describing a 90 degree rotation about the x-axis of
a Cartesian coordinate system.
<BLOCKQUOTE CLASS="quote"><PRE CLASS="verbatim">
:- local array(rotate(3, 3), integer).
:- setval(rotate(0, 0), 1),
   setval(rotate(1, 2), -1),
   setval(rotate(2, 1), 1).
</PRE></BLOCKQUOTE>
(The other elements of the above array are automatically initialised to zero).<BR>
<BR>
The predicate <A HREF="../bips/kernel/storage/current_array-2.html"><B>current_array/2</B></A><A NAME="@default493"></A>
<A NAME="@default494"></A>
is provided to find the size, type and visibility of defined arrays.
of the array and its type to be found:
<BLOCKQUOTE CLASS="quote">
<B>current_array(Array, Props)</B>
</BLOCKQUOTE>
where <I>Array</I> is the array specification as in the declaration (but it
may be uninstantiated or partially instantiated), and <I>Props</I> is
a list indicating the array's type and visibility.
Non-logical variables are also returned, with <I>Array</I> being an atom and their
type is <TT>prolog</TT>.
<BLOCKQUOTE CLASS="quote"><PRE CLASS="verbatim">
[eclipse 1]: local(array(pair(2))),
        setval(count, 3),
        local(array(count(3,4,5), integer)).

yes.
[eclipse 2]: current_array(Array, Props).

Array = pair(2)
Props = [prolog, local]     More? (;) 

Array = count
Props = [prolog, local]     More? (;) 

Array = count(3, 4, 5)
Props = [integer, local]     More? (;) 

no (more) solution.
[eclipse 3]: current_array(count(X,Y,Z), _).

X = 3
Y = 4
Z = 5
yes.
</PRE></BLOCKQUOTE>
<HR>
<A HREF="umsroot054.html"><IMG SRC ="previous_motif.gif" ALT="Previous"></A>
<A HREF="umsroot049.html"><IMG SRC ="contents_motif.gif" ALT="Up"></A>
<A HREF="umsroot056.html"><IMG SRC ="next_motif.gif" ALT="Next"></A>
</BODY>
</HTML>
